import {
  countryCodeOptions,
  formInline,
  helpRef,
  roleOptions,
  drawerInputValidators,
} from "./store";

export const objectToOptions = (obj: any = {}, valueType = "string") => {
  const tmpArray: any = Object.entries(obj);
  const options: any = [];
  for (let [value, label] of tmpArray) {
    if (valueType === "string") value = value + "";
    if (valueType === "number") value = +value;
    options.push({ label, value });
  }
  return options;
};
export const validatorMobile = async (rule: any, value: string) => {
  if (!value) {
    helpRef.value = <i class="fs10">{$t("sysAccount20")}</i>;
    drawerInputValidators[0] = true;
    return Promise.reject();
  }
  if (isEmptyType(formInline.mobile_area)) {
    helpRef.value = <i class="fs10">{$t("placeholder65")}</i>;
    drawerInputValidators[0] = true;
    return Promise.reject();
  }

  if (value.length < 7) {
    helpRef.value = <i class="fs10">{$t("sysAccount39")}</i>;
    drawerInputValidators[0] = true;
    return Promise.reject();
  }
  helpRef.value = null;
  drawerInputValidators[0] = false;
  return Promise.resolve();
};

export const fetchCountryCodeOptions = async () => {
  try {
    const res: any = await accountApi.queryCountryCode();
    if (res.code != 200) return message.error(res.message);
    countryCodeOptions.value = objectToOptions(res.data);
  } catch (error: any) {
    message.error(error.message);
  }
};

export const fetchRoleDict = async () => {
  try {
    const res: any = await roleApi.query({
      page: 1,
      page_size: 10000,
    });
    if (res.code != 200) return message.error(res.message);
    roleOptions.value =
      res?.data?.list?.map((item: any) => ({
        label: item.name,
        value: item.id,
        desc: item.desc,
      })) ?? [];
  } catch (error: any) {
    message.error(error.message);
  }
};
